/*
 * ItemsSearchDialog.java
 */
package ramosmanuales;

import java.util.ArrayList;
import java.util.ListIterator;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import org.jdesktop.application.Action;
import ramosmanuales.data.xml.Derechohabiente;

/**
 * Search dialog
 * @author arlopez
 */
public class ItemsSearchDialog extends javax.swing.JDialog {

    private static ArrayList<Derechohabiente> itemList;

    /**
     * Creates a new instance of search dialog
     * @param parent
     * @param data
     */
    public ItemsSearchDialog(java.awt.Frame parent, ArrayList<Derechohabiente> data) {
        super(parent);
        initComponents();
        itemList = new ArrayList<Derechohabiente>();
        itemList.addAll(data);
        showData(itemList);
    }

    /**
     * Shows table data
     * @param list de derechohabiente
     */
    private void showData(ArrayList<Derechohabiente> lista) {
        setCellValues(searchTable, lista);
    }

    /**
     * Sets the element data values into cells items table
     * @param table item search table
     * @param data element data as <code>ArrayList</code>
     */
    private void setCellValues(JTable table, ArrayList<Derechohabiente> data) {
        DefaultTableModel defaultModel = (DefaultTableModel) table.getModel();
        Object arrayData[] = {};

        defaultModel.getDataVector().removeAllElements();
        
        if (data != null) {
            for (int i = 0; i < data.size(); i++) {
                if (data.isEmpty()) {
                    break;
                }

                arrayData = data.get(i).getBusquedaElements();

                if (i > table.getRowCount() - 1) {
                    defaultModel.addRow(arrayData);
                }

                for (int j = 0; j < arrayData.length; j++) {
                    if (j == 5) {
                        arrayData[j] = ((Object[]) arrayData[j])[0] + " " + ((Object[]) arrayData[j])[1] + " " + ((Object[]) arrayData[j])[2];
                    }

                    if (j == 6) {
                        Object[] ordenDescuento = (Object[]) ((Object[]) arrayData[6])[0];
                        String year;
                        String fn;
                        table.setValueAt(((Object[]) ordenDescuento)[0], i, 6);
                        table.setValueAt(((Object[]) ordenDescuento)[1], i, 7);
                        year = ((Object[]) ordenDescuento)[2].toString();
                        fn = ((Object[]) ordenDescuento)[3].toString();
                        table.setValueAt(year.concat(fn), i, 8);
                        year = ((Object[]) ordenDescuento)[4].toString();
                        fn = ((Object[]) ordenDescuento)[5].toString();
                        table.setValueAt(year.concat(fn), i, 9);
                        table.setValueAt(((Object[]) ordenDescuento)[6], i, 10);
                        break;
                    }
                    table.setValueAt(arrayData[j], i, j);
                }
            }
        }
        defaultModel.fireTableDataChanged();
        table.repaint();
    }

    /**
     * Iterates items list then shows matched data
     */
    @Action
    public void itemSearch() {
        ArrayList<Derechohabiente> list = new ArrayList<Derechohabiente>();
        ListIterator<Derechohabiente> iterator = itemList.listIterator();
        for (; iterator.hasNext();) {
            Derechohabiente element = iterator.next();
            if (element.getPersona().getNombre().equals(firstNameTextField.getText())) {
                list.add(element);
                continue;
            }
            if (element.getPersona().getAPaterno().equals(lastNameTextField.getText())) {
                list.add(element);
                continue;
            }
            if (element.getPersona().getAMaterno().equals(secondNameTextField.getText())) {
                list.add(element);
                continue;
            }
            if (element.getRfc().equals(rfcTextField.getText())) {
                list.add(element);
                continue;
            }
            if (element.getNoIssste().equals(noIsssteTextField.getText())) {
                list.add(element);
                continue;
            }
        }

        showData(list);
    }

    @Action
    public void closeSearchDialog() {
        dispose();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        secondNameLabel = new javax.swing.JLabel();
        noIsssteTextField = new javax.swing.JTextField();
        lastNameTextField = new javax.swing.JTextField();
        noIsssteLabel = new javax.swing.JLabel();
        firstNameLabel = new javax.swing.JLabel();
        secondNameTextField = new javax.swing.JTextField();
        firstNameTextField = new javax.swing.JTextField();
        searchButton = new javax.swing.JButton();
        lastNameLabel = new javax.swing.JLabel();
        rfcLabel = new javax.swing.JLabel();
        rfcTextField = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        searchTable = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(ramosmanuales.RamosManualesApp.class).getContext().getResourceMap(ItemsSearchDialog.class);
        setTitle(resourceMap.getString("title")); // NOI18N
        setModal(true);
        setName("busquedaBox"); // NOI18N
        setResizable(false);

        jPanel1.setBackground(resourceMap.getColor("jPanel1.background")); // NOI18N
        jPanel1.setForeground(resourceMap.getColor("jPanel1.foreground")); // NOI18N
        jPanel1.setName("jPanel1"); // NOI18N

        secondNameLabel.setFont(resourceMap.getFont("rfcLabel.font")); // NOI18N
        secondNameLabel.setText(resourceMap.getString("secondNameLabel.text")); // NOI18N
        secondNameLabel.setName("secondNameLabel"); // NOI18N

        noIsssteTextField.setName("noIsssteTextField"); // NOI18N

        lastNameTextField.setName("lastNameTextField"); // NOI18N

        noIsssteLabel.setFont(resourceMap.getFont("rfcLabel.font")); // NOI18N
        noIsssteLabel.setText(resourceMap.getString("noIsssteLabel.text")); // NOI18N
        noIsssteLabel.setName("noIsssteLabel"); // NOI18N

        firstNameLabel.setFont(resourceMap.getFont("rfcLabel.font")); // NOI18N
        firstNameLabel.setText(resourceMap.getString("firstNameLabel.text")); // NOI18N
        firstNameLabel.setName("firstNameLabel"); // NOI18N

        secondNameTextField.setName("secondNameTextField"); // NOI18N

        firstNameTextField.setName("firstNameTextField"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(ramosmanuales.RamosManualesApp.class).getContext().getActionMap(ItemsSearchDialog.class, this);
        searchButton.setAction(actionMap.get("buscarRegistro")); // NOI18N
        searchButton.setFont(resourceMap.getFont("rfcLabel.font")); // NOI18N
        searchButton.setText(resourceMap.getString("searchButton.text")); // NOI18N
        searchButton.setName("searchButton"); // NOI18N

        lastNameLabel.setFont(resourceMap.getFont("rfcLabel.font")); // NOI18N
        lastNameLabel.setText(resourceMap.getString("lastNameLabel.text")); // NOI18N
        lastNameLabel.setName("lastNameLabel"); // NOI18N

        rfcLabel.setFont(resourceMap.getFont("rfcLabel.font")); // NOI18N
        rfcLabel.setText(resourceMap.getString("rfcLabel.text")); // NOI18N
        rfcLabel.setName("rfcLabel"); // NOI18N

        rfcTextField.setName("rfcTextField"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        searchTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null}
            },
            new String [] {
                "Delegación", "Ramo", "Pagaduría", "No. ISSSTE", "RFC", "Nombre", "No. Préstamo", "Plazo", "Inicio", "Fin", "Monto"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        searchTable.setName("searchTable"); // NOI18N
        jScrollPane1.setViewportView(searchTable);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(noIsssteLabel)
                        .addGap(2, 2, 2)
                        .addComponent(noIsssteTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(rfcLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(rfcTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 13, Short.MAX_VALUE)
                        .addComponent(firstNameLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(firstNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(lastNameLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lastNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(12, 12, 12)
                        .addComponent(secondNameLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(searchButton, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(secondNameTextField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(42, 42, 42))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 909, Short.MAX_VALUE)
                        .addContainerGap())))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(noIsssteTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(noIsssteLabel)
                    .addComponent(rfcTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(rfcLabel)
                    .addComponent(secondNameLabel)
                    .addComponent(secondNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lastNameLabel)
                    .addComponent(lastNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(firstNameLabel)
                    .addComponent(firstNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(9, 9, 9)
                .addComponent(searchButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(22, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel firstNameLabel;
    private javax.swing.JTextField firstNameTextField;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lastNameLabel;
    private javax.swing.JTextField lastNameTextField;
    private javax.swing.JLabel noIsssteLabel;
    private javax.swing.JTextField noIsssteTextField;
    private javax.swing.JLabel rfcLabel;
    private javax.swing.JTextField rfcTextField;
    private javax.swing.JButton searchButton;
    private javax.swing.JTable searchTable;
    private javax.swing.JLabel secondNameLabel;
    private javax.swing.JTextField secondNameTextField;
    // End of variables declaration//GEN-END:variables
}
